﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.IO;

using XJX.BusinessLayer;

namespace XJX.Page
{
    /// <summary>
    ///Log 的摘要说明
    /// </summary>
    public class Admin
    {
        public Admin() { }

        //
        public static string Ad_Search_Book_From_BID(string BID, out int r)
        {
            string result = "";

            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                dt = bs.Ad_Search_Book_From_BID(BID,out r);//得到内存数据表

                ArrayList data = Common.DataTable2ArrayList(dt);//将上述表的数据转换成数组表形式，单行数据是哈希表格式。

                ht["rows"] = data;//将上述数据再放入一个哈希表中。

                result = XJX.BusinessLayer.JSON.Encode(ht);//将上述哈希表进行格式化
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return result;
        }

        //导出确认信息表
        public static bool Get_Admin_Confirm_Yes_List()
        {
            bool b = false;

            MemoryStream ms = new MemoryStream();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);


            try
            {
                //生成DataTable
                DataTable dt = bs.Get_Admin_Confirm_Yes_List();

                //生成内存流
                ms = XJX.Page.Helper.DataTableToExcel(dt, "共享书库图书送达确认表");

                // 判断指定目录下是否存在文件夹，如果不存在，则创建
                string year = System.DateTime.Now.Year.ToString().Trim();
                string path = HttpContext.Current.Server.MapPath("~\\ExportToExcel\\" + year);
                if (!Directory.Exists(path))
                {
                    // 创建文件夹
                    Directory.CreateDirectory(path);//按年份分目录保存
                }

                //将内存流生成文件。

                //方案一：将数据导出后，保存在服务器上，再通过JSON将文件路径传到前端，再下载。分成两个部分来完成的。
                //XJX.Page.Helper.SaveToFile(ms, path + "\\","共享书库图书送达确认表保存" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".xls");
                //参考：http://www.jb51.net/article/97886.htm （未验证此方法的可行性）

                //方案二：前端指定按钮的客户端函数，调用后端的ashx中的方法来执行，如果需要传递参数，则直接在链接中夹带。只是ashx中的返回值似乎用不上了。
                //参考：https://blog.csdn.net/u013066244/article/details/53930763
                        //function ExcuteFun() {
                        //    window.location.href = "../Handle/AdminHandlerService.ashx?method=Get_Admin_Confirm_Yes_List";
                        //}

                XJX.Page.Helper.SaveToFile(ms, "共享书库图书送达确认表保存" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".xls");

                b = true;
            }
            catch
            {
                b = false;
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }

            return b;
        }

        public static int Confirm_One_Book_Arrived(string ID)
        {
            int r = 0;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                r = bs.Confirm_One_Book_Arrived(ID);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }

            return r;
        }

        public static int Ad_Clear_Confirm_Table_Info()
        {
            int r = 0;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                r = bs.Ad_Clear_Confirm_Table_Info();
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }

            return r;
        }

        //验证管理员登录信息
        public static int Ad_Verification_User_Log(string mobile, string pwd, out string ReturnFieldValue)
        {
            int i;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                i = bs.Ad_Verification_User_Log(mobile, pwd,out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return i;
        }

        //获取管理员信息。输出成类
        public static AdminInfo Ad_2_Get_Admin_Info_0(string schoolID)
        {
            AdminInfo ai = new AdminInfo();

            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                dt = bs.Ad_2_Get_Admin_Info(schoolID);

                foreach (DataRow dr in dt.Rows)
                {
                    ai.ID = dr["id"].ToString();
                    ai.SchoolID = dr["SchoolID"].ToString();
                    ai.AdminName = dr["AdminName"].ToString();
                    ai.ContainLetter = dr["ContainLetter"].ToString();
                    ai.Mobile = dr["Mobile"].ToString();
                    ai.LogNUM = dr["LogNUM"].ToString();
                    ai.LastDate = dr["LastDate"].ToString();
                }
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return ai;
        }

        //获取管理员信息。输出成JSON字符串。
        public static string Ad_2_Get_Admin_Info_1(string schoolID)
        {
            string result = "";

            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Admin bs = new XJX.BusinessLayer.Admin(str);

            try
            {
                dt = bs.Ad_2_Get_Admin_Info(schoolID);

                ArrayList data = Common.DataTable2ArrayList(dt);//将上述表的数据转换成数组表形式，单行数据是哈希表格式。

                ht["rows"] = data;//将上述数据再放入一个哈希表中。

                result = XJX.BusinessLayer.JSON.Encode(ht);//将上述哈希表进行格式化
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }

            return result;
        }
     }

    public class AdminInfo
    {
        public string ID;
        public string SchoolID;
        public string AdminName;
        public string ContainLetter;
        public string Mobile;
        public string LogNUM;
        public string LastDate;
    }


}